package com.wxj.book.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxj.book.cms.dao.MajorMapper;
import com.wxj.book.cms.pojo.Major;
import com.wxj.book.cms.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MajorServiceImpl implements MajorService {

    @Autowired
    private MajorMapper majorMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public PageInfo<Major> getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Major> list = majorMapper.getAll();
        PageInfo<Major> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 添加或者更新
     * @param major
     * @return
     */
    @Override
    public int updateMajor(Major major) {
        if (major.getId() == null) {
            Major newMajor = new Major();
            newMajor.setMajorCode(major.getMajorCode());
            newMajor.setMajorName(major.getMajorName());
            newMajor.setMajorStatus(major.getMajorStatus());
            return majorMapper.insertSelective(newMajor);
        }
        Major newMajor = new Major();
        newMajor.setId(major.getId());
        newMajor.setMajorCode(major.getMajorCode());
        newMajor.setMajorName(major.getMajorName());
        newMajor.setMajorStatus(major.getMajorStatus());
        return majorMapper.updateByPrimaryKeySelective(newMajor);
    }

    /**
     * 删除信息
     * @param majorId
     * @return
     */
    @Override
    public int deleteById(Integer majorId) {
        return majorMapper.deleteByPrimaryKey(majorId);
    }

    /**
     * 查询列表
     * @return
     */
    @Override
    public List<Major> getMajor() {
        return majorMapper.getMajor();
    }
}
